var tweetBar = null;

document.ready(function() {
    setValue('menuid', getUrlParam('menuid'));

    initCopyBar();

    getSortationList();
    queryKey('ok', 'yes');
});

function getSortationList() {
    var param = {};
    param.ctrl = 'tweet';
    param.func = 'getSortationList';

    sendAjax(param, function(result) {
        loadOptionsItem('sortationOptions', 'sortationInfo', result.sortationList);
        setSelectItem('sortation', result.sortationList);

        tweetBar = getHtml('writeBar1');
        setHtml('writeBar1', '');
    });
}

function queryKey(flag, permission = 'no') {
    openLoad();
    resetPageBar(flag);

    var param = {};
    param.ctrl = 'tweet';
    param.func = 'getTweetList';
    param.sortationInfo = getAjaxValue('sortationInfo');
    param.page = getAjaxValue('page');
    param.menuid = getAjaxValue('menuid');
    param.permission = permission;

    sendAjax(param, function(result) {
        tweet.list = result.tweetList;

        if (result.permission != null && result.permission != '') {
            tweet.permission = result.permission;
            setDisplay('createTweetBtn', tweet.permission.createtweet == 'yes' ? '' : 'none');
            setDisplay('deleteAllBtn', tweet.permission.deleteall == 'yes' ? '' : 'none');
        }
        
        loadPageBar(getValue('page'), result.allPages, result.rowCount);
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function createTweet() {
    openFloat('600px', 'auto', '70px', lyFloatLeft, tweetBar);
    setHtml('writeTitle', '标题设置');
}

function createTitle() {
    var sortation = getValue('sortation');
    if (sortation == '') {
        return;
    }

    var id = 'tweetSortation';
    if (!getValue(id).includes(sortation)) {
        if (getValue(id) != '') {
            setValue(id, getValue(id) + '、' + sortation);
        }
        else {
            setValue(id, sortation);
        }
    }

    setValue('tweetTitle', '【生活服务】' + getValue(id) + '信息');
}

function resetTitle() {
    setValue('tweetTitle', '');
    setValue('tweetSortation', '');
    getItem('sortation').options[0].selected = true;
}

function createHtml() {
    if (isEmpty('tweetTitle') || isEmpty('tweetSortation')) {
        showTip('请确定推文的标题与分类');
        return;
    }

    var param = {};
    param.ctrl = 'tweet';
    param.func = 'createTweetHtml';
    param.sortation = getAjaxValue('tweetSortation');

    sendAjax(param, function(result) {
        closeFloat();
        
        openCopyBar(result.html);

        if (result.zipurl != null && result.zipurl != '') {
            toUrl(result.zipurl);
        }
    });
}

function closeWriteDiv() {
    closeFloat();
}

function deleteInfo(infoid) {
    setValue('tempid', infoid);
    openConfirm('确定删除吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'tweet';
        param.func = 'deleteInfo';
        param.infoid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            queryKey('page');
        });
    });
}

function deleteAllInfo() {
    openConfirm('确定全部删除吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'tweet';
        param.func = 'deleteAllInfo';

        sendAjax(param, function(result) {
            queryKey('page');
        });
    });
}